<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class My_Salary
{

    var $CI;
    var $_model;
			
	public function __construct()
    {
      
        $CI =& get_instance();        
        $this->_model = $CI;
		$this->_model->load->database();
		$this->_model->load->model("muser");
		$this->_model->load->helper('date');        
    }



    function makeSalary($Staff_ID,$FromDate,$ToDate)
 {
		
 	$q=$this->_model->muser->getRdata('Staff','Staff_ID',$Staff_ID);	
	$query=$this->_model->muser->getContract($Staff_ID,$FromDate,$ToDate);
	$WorkingDay=round($this->_model->muser->getWorkingDay($FromDate,$ToDate));	
	if ($query=='0') return FALSE;
	if ($query->num_rows()==2){ // have 2 contracth
		
		$row1=$query->first_row();
		$row2=$query->last_row();		
		$basic1=$this->getAnnex($row1);		
		$basic2=$this->getAnnex($row2);
		$PRA1=$this->getAllowance($row1);
		$PRA2=$this->getAllowance($row2);
		
		$time1=$this->_model->muser->getRMonthTime($Staff_ID,$FromDate,$row1->ExpiredDate);
		$time2=$this->_model->muser->getRMonthTime($Staff_ID,$row2->EffectiveDate,$ToDate);		
		
		
		if (($row1->ContractType_ID=='TE-OE') || ($row1->ContractType_ID=='TE-IA') || ($row1->ContractType_ID=='TE-SC'))
			{
			if ( date("d", strtotime($row1->ExpiredDate)) >= 17){
					$data=$this->getSHU('0');				
				}
			}else
		{
					$data=$this->getSHU($basic2);					
		}							
			
		$allow1=$this->_model->muser->getAllowance($Staff_ID,$FromDate);
		$allow1 = str_replace(",", "",$allow1);
		$allow2=$this->_model->muser->getAllowance($Staff_ID,$row2->EffectiveDate);
		$allow2 = str_replace(",", "",$allow2);
		
		$h1=($basic1 + $allow1 +$PRA1)/$WorkingDay/8;
		$h2=($basic2 + $allow2+$PRA2)/$WorkingDay/8;
		
		$over= $this->_model->muser->getOverTime(); // he so tinh thue
		//tien thuong phat
		$PerformanceBonus1=$time1->PerformanceBonus;
		$PerformanceBonus1= str_replace(",", "",$PerformanceBonus1);
		$ComDeduction1=$time1->ComDeduction;
		$ComDeduction1= str_replace(",", "",$ComDeduction1);
		$PerformanceBonus2=$time2->PerformanceBonus;
		$PerformanceBonus2= str_replace(",", "",$PerformanceBonus2);
		$ComDeduction2=$time2->ComDeduction;
		$ComDeduction2= str_replace(",", "",$ComDeduction2);
		//luong tinh thue
		
		$taxed1= ($basic1 +$allow1+$PRA1)*($time1->WorkingDay-$time1->ULDay-$time1->SLDay)/$WorkingDay -  $h1*$time1->HourOff + $PerformanceBonus1+
				($time1->OT_Weekday+$time1->OT_Sunday + $time1->OT_Holiday + $time1->ON_Weekday+ $time1->ON_Saturday+$time1->ON_Sunday+
		 		$time1->ON_Holiday+$time1->Time_Saturday+$time1->OT_Saturday)*$h1;
		$nontax1=$this->getNonTax($h1, $time1, $over);
		
		$taxed2= ($basic2 +$allow2+$PRA2)*($time2->WorkingDay-$time2->ULDay-$time2->SLDay)/$WorkingDay -  $h2*$time2->HourOff + $PerformanceBonus2+
				($time2->OT_Weekday+$time2->OT_Sunday + $time2->OT_Holiday + $time1->ON_Weekday+ $time2->ON_Saturday+$time2->ON_Sunday+
		 		$time2->ON_Holiday+$time2->Time_Saturday+$time2->OT_Saturday)*$h2;
		$nontax2=$this->getNonTax($h2, $time2, $over);
		$unpaid=$this->_model->muser->getUnpaid($Staff_ID); //  PIT so tien mien thue
		
		$E_SHU=$data['E_SHU'];
		$E_SHU= str_replace(",", "",$E_SHU);
		$C_SHU=$data['C_SHU'];
		$C_SHU=str_replace(",","",$C_SHU);		
		
		$data['TotalAllowance']=number_format(($allow1+$PRA1)*$time1->WorkingDay/$WorkingDay + ($allow2+$PRA2)*$time2->WorkingDay/$WorkingDay); 
		$data['FullName']=$q->FullName;
		$data['PIT']=number_format(round($unpaid));
		$data['WorkedDay']=$time1->WorkedDay+$time2->WorkedDay;
		$data['WorkingDay']=$WorkingDay; 
		$data['FromDate']=$FromDate;
		$data['ToDate']=$ToDate;
		$data['Staff_ID']=$Staff_ID;
		$data['PerformanceBonus']=number_format($PerformanceBonus1+$PerformanceBonus2);
		$data['NoneTax']=number_format(round($nontax1 + $nontax2));
		$data['Pretax']=number_format(round($taxed1 + $taxed2 -$E_SHU-$unpaid ));
		$data['ComDeduction']=number_format($ComDeduction1+$ComDeduction2);
		$d= $taxed1 +$taxed2 -$unpaid - $data['E_SHU'] ; // so tien phai chiu thue
	// thue voi doi tuong dac biet	
		if (($row1->ContractType_ID=='TE-IA')||($row1->ContractType_ID=='TE-SC')){
			$type='N';	
		}else{
			$type='Y';}
		 if ($d<=0){	 	
			$data['tax']=0;
			$data['PaySalary']=number_format(round($taxed1 + $taxed2 + $nontax1 +$nontax2 -$data['E_SHU']) -$ComDeduction1- $ComDeduction2 );
			 
		 }else{
		 	$e=$this->getTax(($taxed1 + $taxed2 -$unpaid -$data['E_SHU']),$type); // so tien dong thue		
			$data['Tax']=number_format(round($e));
			$data['PaySalary']=number_format(round($taxed1 +$taxed2 +$nontax1 +$nontax2 -$data['E_SHU']- $e -$ComDeduction1- $ComDeduction2 ));
		 }
		$data['Total']=number_format($taxed1+$taxed2+$nontax1+$nontax2-$ComDeduction1-$ComDeduction2+$C_SHU);		
		
		
	}else{ // if have one contract
		$row=$query->row();
		if ($row->ContractType_ID== 'OP'){
			$data=$this->getSalaryOP($row,$Staff_ID,$FromDate,$ToDate);
			
		}else{
					$unpaid=$this->_model->muser->getUnpaid($Staff_ID);//so tien duoc mien thue
					
					$time=$this->_model->muser->getRMonthTime($Staff_ID,$FromDate,$ToDate);
					
					$basic=$this->getAnnex($row); // luong co ban
					$PRA=$this->getAllowance($row);	
					
					if (($row->ContractType_ID=='TE-OE')||($row->ContractType_ID=='TE-IA') || ($row->ContractType_ID=='TE-SC')){
						$data=$this->getSHU('0');				
							
					}else
					{
								$data=$this->getSHU($basic);					
					}
					$allow=$this->_model->muser->getAllowance($Staff_ID,$FromDate);
					$allow = str_replace(",", "",$allow);
								
					$PerformanceBonus=$time->PerformanceBonus;
					$PerformanceBonus= str_replace(",", "",$PerformanceBonus);
					$ComDeduction=$time->ComDeduction;
					$ComDeduction= str_replace(",", "",$ComDeduction);
					
					$h=($basic+$allow +$PRA)/$time->WorkingDay/8;//luong theo gio
					$taxed= ($basic +$allow+$PRA)*($time->WorkingDay-$time->ULDay-$time->SLDay)/$WorkingDay -  $h*$time->HourOff + $PerformanceBonus+
							($time->OT_Weekday+$time->OT_Sunday + $time->OT_Holiday + $time->ON_Weekday+ $time->ON_Saturday+$time->ON_Sunday+
					 		$time->ON_Holiday+$time->Time_Saturday+$time->OT_Saturday)*$h;
					
					
					$E_SHU=$data['E_SHU'];
					$E_SHU= str_replace(",", "",$E_SHU);
					$C_SHU=$data['C_SHU'];
					$C_SHU=str_replace(",","",$C_SHU);
							
					$over= $this->_model->muser->getOverTime(); // he so tinh thue
					$nontax=$this->getNonTax($h, $time, $over);
					$data['BasicSalary']=number_format($basic);
					$data['FullName']=$q->FullName;
					$data['MonthAllowance']=number_format(round($allow));
					$data['TotalAllowance']=number_format($PRA);
					$data['PIT']=number_format(round($unpaid));
					$data['WorkingDay']=$WorkingDay;
					$data['WorkedDay']= $time->WorkedDay; 
					$data['FromDate']=$FromDate;
					$data['ToDate']=$ToDate;
					$data['Staff_ID']=$Staff_ID;
					$data['PerformanceBonus']=$time->PerformanceBonus;
					$data['NoneTax']=number_format(round($nontax));		
					$data['ComDeduction']=$time->ComDeduction;
					$d= $taxed -$unpaid - $E_SHU ; // so tien phai chiu thue
					
					if (($row->ContractType_ID=='TE-IA')||($row->ContractType_ID=='TE-SC')){
						$type='N';	
					}else{
						$type='Y';}
					 if ($d<=0){	 	
						$data['Tax']=0;
						$data['PaySalary']=number_format(round($taxed + $nontax -$E_SHU) -$ComDeduction );
						$data['Pretax']=0;
						 
					 }else{
					 	$e=$this->getTax(($taxed -$unpaid -$E_SHU),$type); // so tien dong thue		
						$data['Tax']=number_format(round($e));
						$data['PaySalary']=number_format(round($taxed +$nontax -$E_SHU- $e -$ComDeduction ));
						$data['Pretax']=number_format(round($taxed -$E_SHU-$unpaid ));
					 }	
		 			
		}
			
	}
		
		$E_SHU=$data['E_SHU'];
		$E_SHU= str_replace(",", "",$E_SHU);
		$C_SHU=$data['C_SHU'];
		$C_SHU=str_replace(",","",$C_SHU);
		$tax=$data['Tax'];
		$tax=str_replace(",","", $tax);
		$paysalary=$data['PaySalary'];		
		$paysalary=str_replace(",","",$paysalary );
		if ($paysalary<0) $paysalary=0;
		
	$data['Total']=number_format($tax+$paysalary+$C_SHU+$E_SHU);		
	if ($this->_model->muser->checkSalary($Staff_ID,$FromDate,$ToDate)){
		$this->_model->muser->addData('Salary',$data);	
	}else{
		$r=$this->_model->muser->getSalaryID($Staff_ID,$FromDate,$ToDate);
		$this->_model->muser->updateData('Salary','Salary_ID',$r->Salary_ID,$data);
	}
	 
	
	return $data;		
	
 }
// ham tinh luong cua Operator

function getSalaryOP($row,$Staff_ID,$FromDate,$ToDate)
{
		$q=$this->_model->muser->getRdata('Staff','Staff_ID',$Staff_ID);
		$WorkingDay=round($this->_model->muser->getWorkingDay($FromDate,$ToDate));	
		$unpaid=$this->_model->muser->getUnpaid($Staff_ID);//so tien duoc mien thue
		
		$time=$this->_model->muser->getRMonthTime($Staff_ID,$FromDate,$ToDate);
		
		$basic=$this->getAnnex($row); // luong co ban
		$PRA=$this->getAllowance($row);		
		$PC=$basic*$time->F/100*$time->N/4;
		
		$data=$this->getSHU($basic);					
		
		$allow=$this->_model->muser->getAllowance($Staff_ID,$FromDate);
		$allow = str_replace(",", "",$allow);
					
		$PerformanceBonus=$time->PerformanceBonus;
		$PerformanceBonus= str_replace(",", "",$PerformanceBonus);
		$ComDeduction=$time->ComDeduction;
		$ComDeduction= str_replace(",", "",$ComDeduction);
		
		$h=($basic+$allow +$PRA +$PC)/$time->WorkingDay/8;//luong theo gio
		$taxed= ($basic +$allow+$PRA +$PC)*($time->WorkingDay-$time->ULDay-$time->SLDay)/$WorkingDay-  $h*$time->HourOff + $PerformanceBonus+
				($time->OT_Weekday+$time->OT_Sunday + $time->OT_Holiday + $time->ON_Weekday+ $time->ON_Saturday+$time->ON_Sunday+
		 		$time->ON_Holiday+$time->Time_Saturday+$time->OT_Saturday)*$h;
		
		
		$E_SHU=$data['E_SHU'];
		$E_SHU= str_replace(",", "",$E_SHU);
		$C_SHU=$data['C_SHU'];
		$C_SHU=str_replace(",","",$C_SHU);
				
		$over= $this->_model->muser->getOverTime(); // he so tinh thue
		$nontax=$this->getNontaxOP($h, $time, $over);
		$data['BasicSalary']=number_format($basic);
		$data['FullName']=$q->FullName;
		$data['MonthAllowance']=number_format(round($allow)+$PC);
		$data['TotalAllowance']=number_format($PRA);
		$data['PIT']=number_format(round($unpaid));
		$data['WorkingDay']=$WorkingDay;
		$data['WorkedDay']= $time->WorkedDay; 
		$data['FromDate']=$FromDate;
		$data['ToDate']=$ToDate;
		$data['Staff_ID']=$Staff_ID;
		$data['PerformanceBonus']=$time->PerformanceBonus;
		$data['NoneTax']=number_format(round($nontax));		
		$data['ComDeduction']=$time->ComDeduction;
		$d= $taxed -$unpaid - $E_SHU ; // so tien phai chiu thue
		$type='Y';
			 if ($d<=0)
			 {	 	
				$data['Tax']=0;
				$data['PaySalary']=number_format(round($taxed + $nontax -$E_SHU) -$ComDeduction );
				$data['Pretax']=0;
			 
			 }else{
			 	$e=$this->getTax(($taxed -$unpaid -$E_SHU),$type); // so tien dong thue		
				$data['Tax']=number_format(round($e));
				$data['PaySalary']=number_format(round($taxed +$nontax -$E_SHU- $e -$ComDeduction ));
				$data['Pretax']=number_format(round($taxed -$E_SHU-$unpaid ));
			 }	
	
		return $data;
	
}

function getNontaxOP($h,$time,$over){
	$t=0;		
	$t7=($time->Time_Saturday+$time->ON_Saturday)/8;
	$CN=($time->OT_Sunday+$time->ON_Sunday)/8;
	if ($t7<=$time->ULDay){
		$t +=$t7*20/100;	
	}else{
		$t += $time->ULDay*20/100 + ($time->ULDay-$t7)*30/100;
	}
	$t += $CN*50/100;
	$t=$t*8*$h;
	$t +=$time->OT_Weekday*($over->OT_Weekday-100)/100 + $time->OT_Holiday*($over->OT_Holiday-100)/100;
	return $t;
}

function getSHU($str){
	$result=$this->_model->muser->getSHU();
	$amount= $result->E_SI+$result->E_HI+$result->E_UI;
	$amount1= $result->C_SI+$result->C_HI+$result->C_UI;
	$result1=$this->_model->muser->getMax('MiniSalary');	
	$min=$result1->Salary;
	if ($str < 20*$min){
		$amount=$amount*$str/100;
		$amount1=$amount1*$str/100;
		$data=array(
				"E_SI" =>number_format($result->E_SI*$str/100) ,
				"E_HI" =>number_format( $result->E_HI*$str/100) ,
				"E_UI"=>number_format($result->E_UI*$str/100) ,
				"E_SHU"=>number_format($amount) ,
				"C_SI"=>number_format($result->C_SI*$str/100) ,
				"C_HI"=>number_format($result->C_HI*$str/100) ,
				"C_UI"=>number_format($result->C_UI*$str/100) ,
				"C_SHU"=>number_format($amount1)				
		);
			
	}else{
		$amount=$amount*20*$min/100;
		$amount1=$amount1*20*$min/100;
		$data=array(
				"E_SI" => number_format($result->E_SI*$min/5) ,
				"E_HI" => number_format($result->E_HI*$min/5) ,
				"E_UI"=>number_format($result->E_UI*$min/5) ,
				"E_SHU"=>number_format($amount) ,
				"C_SI"=>number_format($result->C_SI*$min/5) ,
				"C_HI"=>number_format($result->C_HI*$min/5) ,
				"C_UI"=>number_format($result->C_UI*$min/5) ,
				"C_SHU"=>number_format($amount1)				
		);	
	}
	return $data;
}
// get none tax
function getNonTax($h,$thoigian,$over){
	 $c=  $h*( $thoigian->OT_Weekday*($over->OT_Weekday-100) + 
		 	$thoigian->OT_Saturday*($over->OT_Saturday-100)+
		 	$thoigian->Time_Saturday*($over->Time_Saturday-100)+
		 	$thoigian->OT_Sunday*($over->OT_Sunday-100)+
		 	$thoigian->OT_Holiday*($over->OT_Holiday-100)+
		 	$thoigian->ON_Weekday*($over->ON_Weekday-100)+
		 	$thoigian->ON_Saturday*($over->ON_Weekday-100)+
		 	$thoigian->ON_Sunday*($over->ON_Sunday-100)+
		 	$thoigian->ON_Holiday*($over->ON_Holiday-100))/100;  //so tien khong phai chiu thue
	return $c;	 	
}


function getTax($str,$type){
	if ($type=='N') return $str/10;
 	$result=$this->_model->muser->getTax();
	$n=0;
	foreach ($result as $row){
		if ($row->To=='end'){
			$n=$n + ($str-$row->From)*$row->Percent/100;
		}else
		{
			$h=$row->To - $row->From;
			if ($str >= $row->To)
			{
				$n=$n + $h * $row->Percent/100;
			}else
			{
				$n=$n + ($str-$row->From)*$row->Percent/100;
				return $n;
			}
		}			
		
	}
	return $n;
 }
function getAnnex($row){
	$result=$this->_model->muser->getAnnex($row->Contract_ID);
	$basic=$this->getBasicSalary($row->WorkLevel);
	if ($result=='0') return ($row->SalaryScale* $basic);
	else{
		return ($result->SalaryScale * $basic);
	}		
 }

function getAllowance($row){
	$result=$this->_model->muser->getWorkLevel($row->WorkLevel);	
	$PA=$result->PositionAllowance;
	$RA=$result->ResponsibilityAllowance;
	$HA=$result->HousingAllowance;
	$PA = str_replace(",", "",$PA);
	
	$RA = str_replace(",", "",$RA);
	$HA = str_replace(",", "",$HA);
	$total=$HA+$PA+$RA;
	return $total;
}

function getBasicSalary($WorkLevel_ID)
 	{
		$result=$this->_model->muser->getWorkLevel($WorkLevel_ID);
		$SBS=$result->StandardBasicSalary;
		$SBS = str_replace(",", "",$SBS);
		return $SBS;
	}

}